import 'package:flutter/material.dart';

enum PageStatus {
  init,
  loading,
  error,
  success,
}

class PageInfo {
  PageInfo(
    this.uniqueId,
    this.pageName,
  );

  factory PageInfo.route(Route route) {
    return PageInfo(route.hashCode, convertPageName(route));
  }
  // 待优化：如何获取Widget
  static const unknown = "unknown";
  static const defaultName = "default_page_name";

  /// id
  final int uniqueId;
  dynamic pageWidget;

  /// 页面名称
  String pageName;

  /// 生成page名称
  static String convertPageName(Route? route) {
    String? dialogDisplayName = _dialogDisplayName(route);
    String pageName =
        dialogDisplayName ?? route?.settings.name ?? PageInfo.unknown;
    if (pageName == '/') {
      pageName = 'firstPage';
    }

    return pageName;
  }

  static String? _dialogDisplayName(Route? route) {
    String? dialogDisplayName;
    if (route?.settings.arguments is Map) {
      var argMap = route!.settings.arguments as Map;
      if (argMap.containsKey('dialogDisplayName')) {
        dialogDisplayName = argMap['dialogDisplayName'];
      }
    }

    return dialogDisplayName;
  }

  /// 判断是否为未知页面，一般是通过Navigator隐式调用产生
  bool isUnknown() {
    if (pageName.isEmpty || pageName.endsWith(unknown)) {
      return true;
    }

    return false;
  }

  /// 页面是否可用
  bool isValidPage() {
    return !isUnknown();
  }

  @override
  // ignore: avoid_equals_and_hash_code_on_mutable_classes
  bool operator ==(Object o) => o is PageInfo && o.uniqueId == uniqueId;

  @override
  // ignore: avoid_equals_and_hash_code_on_mutable_classes
  int get hashCode => uniqueId.hashCode ^ pageName.hashCode;

  @override
  String toString() {
    return '{uniqueId: $uniqueId, pageName: $pageName';
  }
}
